﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameOp : MonoBehaviour {

	// Use this for initialization
	void Start ()
	{
		transform.Find("btn_create_room").GetComponent<Button>().onClick.AddListener(OnCreateRoomBtnClick);
		transform.Find("btn_enter_room").GetComponent<Button>().onClick.AddListener(OnEnterRoomBtnClick);

		KBEngine.Event.registerOut("onCell2ClientMsg", this, "OnGameOpNetMsg");
	}

	public void onNetCreateRoomMsg(List<object> listMsg)
	{
		int errorID = Convert.ToInt32(listMsg[2]);
		if (errorID == ErrorCodeDef.No)
		{
			SceneManager.LoadScene("SceneRoom");
			return;
		}
	}

	void OnDestroy()
	{
		KBEngine.Event.deregisterOut(this);
	}

	public void OnGameOpNetMsg(List<object> listMsg)
	{
		int msgID = Convert.ToInt32(listMsg[0]);
		if(msgID != MsgDef.CELL2CLIENT_HALL_MSG)
		{
			return;
		}

		Int32 subMsg = Convert.ToInt32(listMsg[1]);
		switch (subMsg)
		{
			case RoomDef.SUBMSG_ENTER_ROOM:
				onNetEnterRoomMsg(listMsg);
				break;
			case RoomDef.SUBMSG_CREATE_ROOM:
				onNetCreateRoomMsg(listMsg);
				break;
		}
	}

	void OnCreateRoomBtnClick()
	{
		List<object> msg = new List<object>();
		msg.Add(MsgDef.CLIENT2CELL_HALL_MSG);
		msg.Add(RoomDef.SUBMSG_CREATE_ROOM);
		KBEngine.Event.fireIn("sendCellMsg", msg);

		//IKernel.Instance().sendCellMsg(msg);
	}

	void onNetEnterRoomMsg(List<object> listMsg)
	{
		int errorID = Convert.ToInt32(listMsg[2]);
		if(errorID == ErrorCodeDef.No)
		{
			SceneManager.LoadScene("SceneRoom");
			return;
		}

		Transform trfEnterRoomNum = transform.Find("img_enter_room_num");
		Text btnRoomID = trfEnterRoomNum.Find("txt_room_id").GetComponent<Text>();
		btnRoomID.text = "进入房间错误，请输入正确房号";
	}

	void OnEnterRoomBtnClick()
	{
		Transform trfEnterRoomNum =  transform.Find("img_enter_room_num");

		Text btnRoomID = trfEnterRoomNum.Find("txt_room_id").GetComponent<Text>();
		btnRoomID.text = "";

		trfEnterRoomNum.gameObject.SetActive(true); 
		for (int i = 0; i <= 9; ++i)
		{
			string btnName = String.Format("btn_num{0:D0}", i);
			int num = i;
			Button btnNum = trfEnterRoomNum.Find(btnName).GetComponent<Button>();
			btnNum.onClick.RemoveAllListeners();
			btnNum.onClick.AddListener(
				()=>
				{
					btnRoomID.text += Convert.ToString(num);
				}
			);
		}

		Button btnNumBackspace = trfEnterRoomNum.Find("btn_num_backspace").GetComponent<Button>();
		btnNumBackspace.onClick.RemoveAllListeners();
		btnNumBackspace.onClick.AddListener(
			()=>
			{
				if (btnRoomID.text.Length == 0)
				{
					return;
				}
				btnRoomID.text = btnRoomID.text.Remove(btnRoomID.text.Length - 1);
			}
		);

		Button btnClose = trfEnterRoomNum.Find("btn_close").GetComponent<Button>();
		btnClose.onClick.RemoveAllListeners();
		btnClose.onClick.AddListener(
			() =>
			{
				trfEnterRoomNum.gameObject.SetActive(false);
			}
		);

		Button btnNumConfirm = trfEnterRoomNum.Find("btn_num_confirm").GetComponent<Button>();
		btnNumConfirm.onClick.RemoveAllListeners();
		btnNumConfirm.onClick.AddListener(
			() =>
			{
				if(btnRoomID.text.Length == 0)
				{
					return;
				}

				int roomID = Convert.ToInt32(btnRoomID.text);

				List<object> msg = new List<object>();
				msg.Add(MsgDef.CLIENT2CELL_HALL_MSG);
				msg.Add(RoomDef.SUBMSG_ENTER_ROOM);
				msg.Add(roomID);

				KBEngine.Event.fireIn("sendCellMsg", msg);

				//IKernel.Instance().sendCellMsg(msg);
			}
		);
	}

}
